package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.business.interfaces.report.ReportPayBService;
import com.zmn.oms.common.constant.MessageQueueTagConsts;
import com.zmn.oms.model.entity.report.EsReportPayQuery;
import com.zmn.oms.model.entity.work.OrderWorkQuery;
import com.zmn.oms.model.query.redistribute.OrderWorkRedistributeQuery;
import com.zmn.oms.zmn.business.interfaces.redistribute.OrderWorkRedistributeInfoBService;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkQueryExportBService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 类描述：工单导出
 *
 * @author wangxiaokun
 * @date 2021/07/20 16:53
 */
@AliyunMQListener(topic = "${spring.mq.topic.oms}")
@Component
public class OrderAsyncExportListener {

    private static final Logger logger = LoggerFactory.getLogger(OrderAsyncExportListener.class);

    @Autowired
    private ZsOrderWorkQueryExportBService zsOrderWorkQueryExportBService;
    @Autowired
    private ReportPayBService reportPayBService;
    @Resource
    private OrderWorkRedistributeInfoBService redistributeInfoBService;

    /**
     * 订单列表导出
     * @param message
     */
    @AliyunMQHandler(tags = MessageQueueTagConsts.ORDER_ASYNC_EXPORT)
    public void orderAsyncExport(String message){

        try {
            logger.debug("#mq#order#async#export#recive message:{}", message);
            OrderWorkQuery query = JSON.parseObject(message, OrderWorkQuery.class);
            zsOrderWorkQueryExportBService.asyncExportListOrderWorkByQuery(query);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * 支付明细导出
     * @param message
     */
    @AliyunMQHandler(tags = MessageQueueTagConsts.ORDER_PAY_ASYNC_EXPORT)
    public void orderPayAsyncExport(String message){

        try {
            logger.debug("#mq#order#pay#async#export#recive message:{}", message);
            EsReportPayQuery query = JSON.parseObject(message, EsReportPayQuery.class);
            reportPayBService.asyncExportPayListByQuery(query);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * 改派列表导出
     * @param message
     */
    @AliyunMQHandler(tags = MessageQueueTagConsts.REDISTRIBUTE_APPLY_ASYNC_EXPORT)
    public void redistributeApplyAsyncExport(String message){

        try {
            logger.debug("#mq#redistribute#async#export#recive message:{}", message);
            OrderWorkRedistributeQuery query = JSON.parseObject(message, OrderWorkRedistributeQuery.class);
            redistributeInfoBService.asyncExportListRedistributeByQuery(query);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
}
